package com.lcxw.hxzbadmin.controller.unioncontroller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcxw.hxzbadmin.entity.requestbean.DealUnionRegistReq;
import com.lcxw.hxzbadmin.entity.responseBean.MemberInfoBase;
import com.lcxw.hxzbapi.entity.pojo.Union;
import com.lcxw.hxzbapi.entity.pojo.UnionMember;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.UnionMemberService;
import com.lcxw.hxzbapi.service.UnionService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbcommon.common.AesCBCUtil;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UnionRealization {

    @Resource
    UnionService unionService;

    @Resource
    UnionMemberService unionMemberService;

    @Resource
    YwpdMemberService ywpdMemberService;

    //创建工会方法
    public Response createUnion(String telephone) throws Exception {

        if (unionService.selectByTelephone(telephone) != null) {
            return Response.error(ErrorCode.ALREADY_EXIST, "该手机号已经创建过工会");
        }

        Union union = new Union();
        AesCBCUtil aesCBCUtil = AesCBCUtil.getInstance();
        String password = aesCBCUtil.encrypt("123456");
        union.setTelephone(telephone);
        union.setPassword(password);
        union.setStatus("初始化");
        union.setUnionName(telephone + "初始化工会");
        union.setInfo(telephone + "初始化工会");
        union.setImg(
            "https://ywpd-picture.obs.cn-east-2.myhuaweicloud.com:443/union/pic/union_1_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20200910115913.jpg?AccessKeyId=TEPZ5RHAPORK7NWI4AI9&Expires=1694318371&Signature=So9SnxaHo1ffah%2F9%2FGfLpxHbIsE%3D");
        union.setAddTime(StringUtil.getDateTime());

        try {
            unionService.insert(union);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(log, "创建工会");
        }
        log.info("为手机号" + telephone + "创建工会成功");

        return Response.ok();

    }

    public Response shutdownUnion(int unionId, String shtReason) {

        Union union = unionService.selectById(unionId);
        union.setStatus("解散");
        union.setShtReason(shtReason);
        union.setShTime(StringUtil.getDateTime());
        unionService.update(union);
        log.warn("公会" + unionId + "已经被解散");
        return Response.ok();
    }

    public Response getUnionList(String telephone, int pageNum, int pageSize) {

        JSONObject data = unionService.getAll(telephone, pageNum, pageSize);
        return Response.ok(data);
    }

    public Response getUnionMembers(int unionId, int pageNum, int pageSize) {
        IPage<UnionMember> unionMembers = unionMemberService
            .getUnionMembers(unionId, null, pageNum, pageSize);
        List<UnionMember> records = unionMembers.getRecords();
        List<MemberInfoBase> memberInfoBaseList = new ArrayList<>();
        for (UnionMember unionMember : records
        ) {
            MemberInfoBase memberInfoBase = new MemberInfoBase();
            YwpdMember ywpdMember = ywpdMemberService
                .selectYwpdMemberByUserId(unionMember.getUserId());
            BeanUtils.copyProperties(ywpdMember, memberInfoBase);
            memberInfoBaseList.add(memberInfoBase);
        }

        JSONObject data = new JSONObject();
        data.put("total", unionMembers.getTotal());
        data.put("records", memberInfoBaseList);
        return Response.ok(data);
    }

    public Response dealUnionRegist(DealUnionRegistReq dealUnionRegistReq) {
        int unionId = dealUnionRegistReq.getUnionId();
        String decision = dealUnionRegistReq.getDecision();
        String reason = dealUnionRegistReq.getReason();
        if (decision.equals("refuse") && StringUtil.isEmpty(reason)) {
            return Response.error(ErrorCode.INVALID_PARAM, "拒绝注册必须携带原因");
        }
        if (decision.equals("agree")) {
            Union union = unionService.selectById(unionId);
            union.setStatus("正常");
            union.setAddTime(StringUtil.getDateTime());
            unionService.update(union);
        }
        if (decision.equals("refuse")) {
            Union union = unionService.selectById(unionId);
            union.setStatus("未通过");
            union.setReason(reason);
            unionService.update(union);
        }
        return Response.ok();
    }
}
